package com.example.jd.finance.web;


import com.example.jd.common.InvokeResultUtil;
import com.example.jd.common.SequenceNumUtil;
import com.example.jd.common.enums.BizStatusCodeEnum;
import com.example.jd.finance.entity.MsCustomerRecords;
import com.example.jd.finance.entity.param.QueryApplyFriendsParam;
import com.example.jd.finance.sever.MsCustomerRecordsService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-11-06
 */
@RestController
@Slf4j
@RequestMapping("/msCustomerRecords")
public class MsCustomerRecordsController {

    @Autowired
    private MsCustomerRecordsService msCustomerRecordsService;

    @Autowired
    SequenceNumUtil sequenceNumUtil;

    /**
     * 得助 上报 信息
     */
    @PostMapping("upload")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "得助上报微信号信息接口")
    @ApiImplicitParam(name = "list", dataType = "list", required = true, value = "上报信息集合", defaultValue = "")
    public InvokeResultUtil<Object> uploadRecords(@RequestBody List<MsCustomerRecords> list) {
        log.info("MsCustomerRecordsController--得助上报接口uploadRecords入参为：{}", list);
        try {
            if (CollectionUtils.isEmpty(list)) {
                return InvokeResultUtil.failure(BizStatusCodeEnum.MISSING_PARAMS.getCode(), "请求参数为空");
            }
            final String orderNum = sequenceNumUtil.generateOrderNum();
            List<MsCustomerRecords> listNew = new ArrayList<>();

            for (MsCustomerRecords e : list) {
                if (e == null || ObjectUtils.isEmpty(e.getCustomerEncryptWechatCode())){
                    continue;
                }
                listNew.add(MsCustomerRecords.generateMsCustomerRecords(sequenceNumUtil.getNum(),
                        orderNum,e.getCallId(), e.getCustomerId(), e.getCustomerEncryptWechatCode()));
            }

            msCustomerRecordsService.saveBatch(listNew);
            return InvokeResultUtil.success("上报成功");
        } catch (Exception e) {
            log.error("得助上报接口uploadRecords异常信息为", e);
            return InvokeResultUtil.failure(BizStatusCodeEnum.ERROR_SYSTEM.getCode(), "上报信息出现异常");
        }
    }


}

